/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Jun 27, 2012, 12:26:50 AM
 */
package game1;

import java.awt.Button;
import java.awt.Color;
import java.awt.Desktop.Action;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 *
 * @author Oho
 */
public class NewJPanel extends javax.swing.JPanel {

    private BufferedImage backgroundImage;

    /**
     * Creates new form NewJPanel
     */
    public void CreateImg() {


        URL url = this.getClass().getResource("/Images/1.jpg");
        try {
            setBackgroundImage(ImageIO.read(url));
        } catch (IOException ex) {
            Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
        }


        // set the panel size to the dimension of the background image
        int panelWidth = getBackgroundImage().getWidth(null);
        int panelHeight = getBackgroundImage().getHeight(null);
        setPreferredSize(new java.awt.Dimension(panelWidth, panelHeight));
    }

    public static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        double scaleX = (double) width / imageWidth;
        double scaleY = (double) height / imageHeight;
        AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

        return bilinearScaleOp.filter(
                image,
                new BufferedImage(width, height, image.getType()));
    }

    public NewJPanel() {
        initComponents();

        //lb.setText("dafdf");
        CreateImg();

    }

    @Override
    public void paintComponent(Graphics g) {
        // super.paint(g);


        //  backgroundImage=  makeTransparent(backgroundImage);
        g.drawImage(getBackgroundImage(), 0, 0, this);
        //  g.drawImage(sourceImage, 0, 0, this);

    }

    //g.drawImage(sourceImage,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,this);
    /**
     * Creates new form NewJPanel
     */
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE));
    }// </editor-fold>                        

    private void fail(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private BufferedImage makeTransparent(BufferedImage sourceImage) {

        int h = sourceImage.getHeight(null);
        int w = sourceImage.getWidth(null);

        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

        // assume the upperleft corner of the original image is a transparent pixel
        int transparentColor = sourceImage.getRGB(0, 0);
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int color = sourceImage.getRGB(x, y);
                if (color == transparentColor) {
                    color = color & 0x00FFFFFF; // clear the alpha flag
                }
                resultImage.setRGB(x, y, color);
            }
        }

        return resultImage;

    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    /**
     * @return the backgroundImage
     */
    public BufferedImage getBackgroundImage() {
        return backgroundImage;
    }

    /**
     * @param backgroundImage the backgroundImage to set
     */
    public void setBackgroundImage(BufferedImage backgroundImage) {
        this.backgroundImage = backgroundImage;
    }
}
